﻿using System;

namespace Xc.DataAccess.Core
{
    /// <summary>
    /// 执行结果
    /// </summary>
    public class Result
    {
        public Result()
        {

        }
        /// <summary>
        /// 是否成功
        /// </summary>
        public bool IsSuccess { get; set; }
        /// <summary>
        /// 是否失败
        /// </summary>
        public bool IsError => !IsSuccess;
        /// <summary>
        ///消息
        /// </summary>
        public string Message { get; set; }
        /// <summary>
        /// 成功
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        public static Result Accept(string message)
        {
            return new Result { IsSuccess = true, Message = message };
        }
        /// <summary>
        /// 失败
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        public static Result Reject(string message)
        {
            return new Result { IsSuccess = false, Message = message };
        }
        /// <summary>
        /// 使用整数判断
        /// </summary>
        /// <param name="number"></param>
        /// <param name="validate"></param>
        /// <returns></returns>
        public static Result From<T>(T number,Func<T,bool> validate)
        {
            bool res = false;
            if (validate!=null)
            {
                res =  validate.Invoke(number);
            }
            else
            {
                res = number?.ToString() == default(T).ToString();
            }
            if (res==true)
            {
                return Result.Accept("成功");
            }
            else
            {
                return Result.Reject("失败");
            }
        }
    }
}
